//
//  HMWelcomeViewController.swift
//  GuoGuo
//
//  Created by 艾小新 on 16/8/5.
//  Copyright © 2016年 xiaoxin. All rights reserved.
//

import UIKit
import SDWebImage

class HMWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()

    }
    
    
    // MARK: -初始化页面
    private func setupUI() {
        
        // 初始化页面颜色
        view.backgroundColor = UIColor.whiteColor()
        
        // 添加子控件到页面
        view.addSubview(imageView)
        view.addSubview(message)
        
        // 设置约束
        imageView.snp_makeConstraints { (make) in
            
            // 宽高是90，中心居中 距离上面200
            
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSizeMake(90, 90))
            make.centerX.equalTo(view)
        }
        
        message.snp_makeConstraints { (make) in
            make.top.equalTo(imageView.snp_bottom).offset(20)
            make.centerX.equalTo(imageView)
            
        }
        
        
   
        
    }
    
    // MARK: -动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 更新约束
        imageView.snp_updateConstraints { (make) in
            make.top.equalTo(100)
        }
        
        /// 缓慢上下弹簧动画
        ///
        /// - parameter <Tduration:             持续时间
        /// - parameter delay:                  延迟
        /// - parameter usingSpringWithDamping: 0-1 弹簧感觉
        /// - parameter initialSpringVelocity:  初始速度 0
        /// - parameter options:                传空
        /// - parameter animations:
        /// - parameter completion:
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            // 重新布局
            self.view.layoutIfNeeded()
            }) { (success) in
                // 弹出 Main页面
                UIView.animateWithDuration(1.0, animations: { 
                    self.message.alpha = 1.0
                    }, completion: { (_) in
                        // 跳转
                        NSNotificationCenter.defaultCenter().postNotificationName(HMLogonViewChangeNotification, object: self)

                })
                
                
                
                
        }
        
        
    }
    
    // MARK: -懒加载
    private lazy var imageView :UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        let name = HMUserLoginViewModel.sharedUserLoginViewModel.account?.avatar_large
        let url = NSURL(string: name!)
        imageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        
        // 切成圆角
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        
        return imageView
        
    }()
    
    private lazy var message: UILabel = {
        
        let name = HMUserLoginViewModel.sharedUserLoginViewModel.account?.screen_name
        
        let v = UILabel(title:"\(name!),欢迎来到艾小微博", color: UIColor.darkGrayColor())
        
        v.sizeToFit()
        
        v.font = UIFont.systemFontOfSize(15)
        
        v.alpha = 0

        return v
        
        
    }()
    
    
    
    
    
}
